package gruppoD;

import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

public class HTTPInterpreter {
	private String path;
	private HashMap<String, String> parameters;

	public HTTPInterpreter(List<String> HTTPRequest) {
		for (int i = 0; i < HTTPRequest.size(); i++) {
			String line = HTTPRequest.get(i);
			StringTokenizer tokenizer = new StringTokenizer(line);
			String token;
			while (tokenizer.hasMoreTokens()) {
				token = tokenizer.nextToken();
				if (token.compareToIgnoreCase("get") == 0) {
					if (tokenizer.hasMoreElements()) {
						path = tokenizer.nextToken();
						System.out.println(path);
					}
				}
			}
		}
		int i = path.indexOf('?');
		if (i == -1) {
			parameters = null;
		} else {
			String substring = path.substring(i + 1);
			StringTokenizer tokenizer = new StringTokenizer(substring);
			String token;
			parameters = new HashMap<String, String>();
			while (tokenizer.hasMoreElements()) {
				token = tokenizer.nextToken("&");
				int index = token.indexOf('=');
				if (index != -1) {
					parameters.put(token.substring(0, index),
							token.substring(index + 1));
					// System.out.println(token.substring(0,
					// index)+"="+token.substring(index+1));
				}
			}
		}
	}

	public HashMap<String, String> getParameters() {
		return parameters;
	}

	public String getPath() {
		return path;
	}
}
